﻿; (function ($) {
    //判断打开位置
    if (self.location == top.location && self.location.toString().indexOf('web/main.aspx') < 0)
        $.post("/web/ajax/main.aspx", {
            tag: "logout"
        }, function () {
            top.location.href = "/index.aspx";
        })
    //美化滚动条
    $("html").niceScroll({ styler: "fb", cursorcolor: "#e8403f", cursorwidth: '6', cursorborderradius: '10px', background: '#404040', spacebarenabled: false, cursorborder: '', zindex: '1000' });

    $.extend({
        show_alert: show_alert,
        show_alert_no_ok: show_alert_no_ok,
        show_table: show_table
    })

    function show_alert(tips, callback) {
        if (tips == "session_out!") {
            toastr.warning('当前登录已失效，请重新登录！', '提示');
            setTimeout("top.location.reload()", 1000);
        }
        else if (tips != "ok")
            toastr.warning(tips, '警告');
        else if (callback)
            callback();
    }
    function show_alert_no_ok(tips, callback) {
        if (tips == "session_out!") {
            toastr.warning('当前登录已失效，请重新登录！', '提示');
            setTimeout("top.location.reload()", 1000);
        }
        else if (callback)
            callback();
    }
    function show_table(pageindex, search, ajax_href, in_href) {
        $('#rf_tblist').dataTable({
            "bProcessing": true,
            "bPaginate": true,
            "bServerSide": true,
            "bLengthChange": false,
            "bStateSave": false,
            "bSort": false,
            "sAjaxSource": ajax_href + "?tag=list",
            "sPaginationType": "full_numbers",
            "sPaginationType": "bootstrap",
            "iDisplayStart": 10 * (pageindex - 1),
            "oSearch": { "sSearch": search, "bRegex": false, "bSmart": true },
            "oLanguage": {
                "sProcessing": "<div style='width:100%;height:100%;text-align:center;'><i class='fa fa-4x fa-spinner fa-spin text-primary'></i></div>",
                "sZeroRecords": "<div style='text-align:center;width:100%'><span style='color:red'>暂无数据!</span></div>",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "从 0 到 0 /共 0 条数据",
                "sInfoFiltered": "",
                "sSearch": "搜索",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上一页",
                    "sNext": "下一页",
                    "sLast": "末页"
                }
            },
            "sDom": '<"toolbar">frtip',
            "fnServerData": function (sUrl, aoData, fnCallback, oSettings) {
                oSettings.jqXHR = $.ajax({
                    "url": sUrl,
                    "data": aoData,
                    "success": function (json) {
                        oSettings._iDisplayStart = parseInt(json.iDisplayStart);
                        $(".rf_btn_add").attr("href", in_href + "?p=" + (oSettings._iDisplayStart / oSettings._iDisplayLength + 1) + "&kw=" + oSettings.oPreviousSearch.sSearch)
                        fnCallback(json);
                    },
                    "dataType": "json",
                    "cache": false,
                    "type": oSettings.sServerMethod,
                    "error": function () {
                        toastr.warning("当前登录已失效，请重新登陆！");
                        setTimeout('top.location.href = "/index.aspx"', 1000);
                    }
                });
            }
        });
        if (in_href && $.trim(in_href) != "")
            $("div.toolbar").html('<a href="' + in_href + '?p=' + pageindex + '&kw=' + search + '" class="btn btn-primary rf_btn_add"><i class="fa fa-plus"></i>&nbsp;新增</a>');
        else
            $("div.toolbar").html('');
    }
    $(document).on("click", ".rf_btn_delete", function () {
        toastr.confirm("确定删除该信息吗？<input class='rf_toastr_tips' type='hidden' data-id='" + $(this).data("id") + "' data-href='" + $(this).data("href") + "'></input>", "警告");
    })
    $(document).on("click", "#toastr_ok", function () {
        if ($("#toast-container .rf_toastr_tips").length > 0) {
            var $btn = $("#toast-container .rf_toastr_tips");
            $.post($btn.data("href"), {
                id: $btn.data("id")
            }, function (result) {
                if (result == "session_out!") {
                    toastr.warning('当前登录已失效，请重新登录！', '提示');
                    setTimeout("top.location.reload()", 1000);
                }
                else if (result != "ok")
                    toastr.warning(result, '警告');
                else {
                    $(".dataTables_paginate>ul>li.active>a").click();
                }
            })
        }
    })
})(jQuery);